﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

/// <summary>
/// 
/// </summary>
public static class CommonExtenders
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string ToNString(this string value)
    {
        if (string.IsNullOrEmpty(value))
        {
            return string.Empty;
        }
        else
        {
            return value;
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string ToNString(this object value)
    {
        if (value == null)
        {
            return string.Empty;
        }
        else
        {
            return value.ToString();
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static int ToInt(this string value)
    {
        int i = 0;

        if (string.IsNullOrEmpty(value))
            return 0;

        if (int.TryParse(value, out i))
        {
            return i;
        }
        else
        {
            return 0;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static int ToInt(this string value, int defaultValue)
    {
        int i = 0;

        if (string.IsNullOrEmpty(value))
            return defaultValue;

        if (int.TryParse(value, out i))
        {
            return i;
        }
        else
        {
            return defaultValue;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static int ToInt(this object value)
    {
        return (value == null ? 0 : Convert.ToInt32(value, CultureInfo.InvariantCulture));
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static decimal ToDecimal(this object value)
    {
        return (value == null ? 0 : Convert.ToDecimal(value, CultureInfo.InvariantCulture));
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static decimal ToDecimal(this string value, decimal defaultValue)
    {
        decimal i = 0;

        if (string.IsNullOrEmpty(value))
            return defaultValue;

        if (decimal.TryParse(value, out i))
        {
            return i;
        }
        else
        {
            return defaultValue;
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static long ToLong(this string value)
    {
        long i = 0;

        if (string.IsNullOrEmpty(value))
            return 0;

        if (long.TryParse(value, out i))
        {
            return i;
        }
        else
        {
            return 0;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static long ToLong(this string value, int defaultValue)
    {
        long i = 0;

        if (string.IsNullOrEmpty(value))
            return defaultValue;

        if (long.TryParse(value, out i))
        {
            return i;
        }
        else
        {
            return defaultValue;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static byte ToByte(this string value)
    {
        byte i = 0;

        if (string.IsNullOrEmpty(value))
            return 0;

        if (byte.TryParse(value, out i))
        {
            return i;
        }
        else
        {
            return 0;
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static byte ToByte(this object value)
    {
        return (value == null ? (byte)0 : Convert.ToByte(value, CultureInfo.InvariantCulture));
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static double ToDouble(this string value)
    {
        double i = 0;

        if (string.IsNullOrEmpty(value))
            return 0;

        if (double.TryParse(value, out i))
        {
            return i;
        }
        else
        {
            return 0;
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static double ToDouble(this object value)
    {
        return (value == null ? 0 : Convert.ToDouble(value, CultureInfo.InvariantCulture));
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static DateTime ToDateTime(this object value)
    {
        return (value == null ? DateTime.Now : Convert.ToDateTime(value, CultureInfo.InvariantCulture));
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static object ToDBNull(this int value)
    {
        if (value == 0)
            return DBNull.Value;
        else
            return value;
    }
    public static bool ToBool(this string value)
    {
        bool b;

        if (string.IsNullOrEmpty(value))
            return false;

        if (bool.TryParse(value, out b))
        {
            return b;
        }
        else
        {
            return false;
        }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="values"></param>
    /// <returns></returns>
    public static string ToDelimitedString(this int[] values)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < values.Length; i++)
            sb.Append(values[i] + ",");
        return sb.ToString().TrimEnd(',');
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="values"></param>
    /// <returns></returns>
    public static int[] ToIntArray(this string values)
    {
        string[] arr = { };
        int[] r = { };
        if (string.IsNullOrEmpty(values))
        {
            return r;
        }
        else
        {
            arr = values.Trim(',').Split(',');
            r = new int[arr.Length];
            for (int i = 0; i < arr.Length; i++)
                r[i] = arr[i].ToInt();
            return r;
        }
    }
}